//
//  TVSignalModeView.swift
//  StarTimesON
//
//  Created by Toj on 8/25/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

private let signalViewHeight: CGFloat = 24.ui_scale

class TVSignalModeView: UIView {
    
    var model: ProductVO? { didSet {
        guard let product = model else { return }
        
        modeIcon.image = product.tvPlatform.icon.to_image
        nameButton.setTitle(product.name, for: .normal)
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
    }
    
    @objc private func didTapName() {
        
        guard let pack = model else { return }
        let vc = STSChannelDetailListController()
        vc.rquestData(title:pack.name, packageId: pack.packageId)
        UIViewController.topMost?.navigationController?.pushViewController(vc, animated: true)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    // 信号模式
    private lazy var modeIcon = UIImageView()
    
    // 信号名称背景
    private lazy var nameView: UIView = {
        let view = UIView()
        view.st_cornerRadius = signalViewHeight * 0.5
        view.backgroundColor = "#FFFFFF".to_color.st_alpha(0.5)
        return view
    }()
    
    // 信号名称
    lazy var nameButton: UIButton = {
        let view = UIButton()
        view.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14.ui_scale)
        view.setTitleColor( "#E68100".to_color, for: .normal)
        view.addTarget(self, action: #selector(didTapName), for: .touchUpInside)
        return view
    }()
}

// MARK: - 初始化
extension TVSignalModeView {
    
    func setupMakeAddSubviews() {
        addSubview(modeIcon)
        addSubview(nameView)
        addSubview(nameButton)
    }
    
    private func setupMakeLayoutSubviews() {
        
        modeIcon.snp.makeConstraints { make in
            make.left.centerY.equalTo(self)
            make.height.width.equalTo(24.ui_scale)
        }
        
        nameButton.snp.makeConstraints { make in
            make.centerY.equalTo(modeIcon)
            make.left.equalTo(36.ui_scale)
            make.right.equalTo(snp.right).offset(-8.ui_scale)
            make.height.equalTo(self)
        }
        
        nameView.snp.makeConstraints { make in
            make.left.equalTo(nameButton.snp.left).offset(-8.ui_scale)
            make.right.equalTo(nameButton.snp.right).offset(8.ui_scale)
            make.centerY.equalTo(nameButton)
            make.height.equalTo(signalViewHeight)
        }
    }
}
